package com.zyj.eps.controller;

import com.alibaba.fastjson.JSON;
import com.zyj.eps.pojo.Manager;
import com.zyj.eps.pojo.User;
import com.zyj.eps.service.ManagerServiceImpl;
import com.zyj.eps.service.UserServiceImpl;
import com.zyj.eps.util.IDutil;
import com.zyj.eps.util.JwtHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author super
 * @date 2022/3/26 17:18
 */
@Controller
@RestController
@CrossOrigin
@SuppressWarnings("all")//抑制警告
public class LoginController {
    @Autowired
    /*@Qualifier("UserServiceImpl")*/ //是按照名字查找注册的bean
    private UserServiceImpl userService;
    @Autowired
    private ManagerServiceImpl managerService;

    @RequestMapping("/login")
    public String getAllUser(@RequestBody User user){
        System.out.println("访问成功"+user);
        String token = "";
        String flag = "error";
        User us = userService.Login(user.getUname(),user.getPassword());
        HashMap<String, Object> res = new HashMap<>();
        System.out.println("users:" + us);
        if (us != null) {
            flag = "ok";
            String a = ""+us.getUid();
            token = JwtHelper.createToken(a);
        }
        System.out.println("token:"+token);
        res.put("token",token);
        res.put("flag",flag);
        res.put("User" ,us);
        return JSON.toJSONString(res);
    }
    @RequestMapping("/register")
    public String Register(@RequestBody User user){
        System.out.println("访问成功，注册用户");
        String token = "";
        String flag = "error";
        user.setUserID(IDutil.GetUUID());
        userService.register(user);
        User us = userService.Login(user.getUname(),user.getPassword());
        if(us!=null){
            flag="ok";
            token = JwtHelper.createToken(us.getUserID());
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("flag",flag);
        data.put("token",token);
        data.put("User" ,us);
        return JSON.toJSONString(data);
    }
    @RequestMapping("/AdminLogin")
    public String AdminLogin(@RequestBody Manager manager){
        String flag = "error";
        Manager manager1 = managerService.ManagerLogin(manager.getMname(), manager.getPassword());
        HashMap<String, Object> res = new HashMap<>();
        System.out.println("Manager:" + manager1);
        if (manager1 != null) {
            flag = "ok";
        }
        res.put("flag",flag);
        res.put("Manager" ,manager1);
        return JSON.toJSONString(res);
    }

}
